package com.xwd.web.student;

import com.xwd.domain.Student;
import com.xwd.service.StudentService;
import com.xwd.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/subUpdate")
public class SubUpdateServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        Student student = new Student();
        student.setStuId(Integer.parseInt(req.getParameter("stuId")));
        student.setStuName(req.getParameter("stuName"));
        student.setStuNo(req.getParameter("stuNo"));
        student.setSex(Integer.parseInt(req.getParameter("sex")));
        student.setGid(Integer.parseInt(req.getParameter("gid")));
        student.setPhone(req.getParameter("phone"));
        student.setEmail(req.getParameter("email"));
        student.setRegistered(req.getParameter("registered"));
        student.setAddress(req.getParameter("address"));
        student.setProfession(req.getParameter("profession"));
        student.setIdNumber(req.getParameter("idNumber"));
        student.setPolitics(req.getParameter("politics"));
        student.setIntroduction(req.getParameter("introduction"));

        //调用service
        StudentService service =new StudentServiceImpl();
        int i = service.updateStudent(student);


        //跳转页面
        PrintWriter writer = resp.getWriter();
        if(i>0 ){
            writer.println("<script> alert('修改成功'); location.href='/getStudetList' </script>");
        }else{
            writer.println("<script> alert('修改失败，请重试'); location.href=\'/getupdateStudent?stuId="+Integer.parseInt(req.getParameter("stuId"))+"\'</script>");
        }
    }
}
